package com.ruoyi.system.service.impl;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.SysSort;
import com.ruoyi.system.mapper.SysSortMapper;
import com.ruoyi.system.mapper.SysTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ArSavePolicyMapper;
import com.ruoyi.system.domain.ArSavePolicy;
import com.ruoyi.system.service.IArSavePolicyService;

/**
 * 存储策略信息Service业务层处理
 * 
 * @author ruoyi_ln
 * @date 2022-03-01
 */
@Service
public class ArSavePolicyServiceImpl implements IArSavePolicyService 
{
    @Autowired
    private ArSavePolicyMapper arSavePolicyMapper;

    @Autowired
    private SysSortMapper sysSortMapper;

    @Autowired
    private SysTableMapper sysTableMapper;

    /**
     * 查询存储策略信息
     * 
     * @param recNo 存储策略信息ID
     * @return 存储策略信息
     */
    @Override
    public ArSavePolicy selectArSavePolicyById(String recNo)
    {
        return arSavePolicyMapper.selectArSavePolicyById(recNo);
    }

    /**
     * 查询存储策略信息列表
     * 
     * @param arSavePolicy 存储策略信息
     * @return 存储策略信息
     */
    @Override
    public List<ArSavePolicy> selectArSavePolicyList(ArSavePolicy arSavePolicy)
    {
        return arSavePolicyMapper.selectArSavePolicyList(arSavePolicy);
    }

    /**
     * 新增存储策略信息
     * 
     * @param arSavePolicy 存储策略信息
     * @return 结果
     */
    @Override
    public AjaxResult insertArSavePolicy(ArSavePolicy arSavePolicy)
    {
        // 验证是否有主表
        SysSort sysSort = sysSortMapper.selectSysSortById(arSavePolicy.getSortNo());
        if (sysSort.getSortMainTableNo().isEmpty()){
            return AjaxResult.error("请先设置主表");
        }
        String tableName = sysTableMapper.selectSysTableById(sysSort.getSortMainTableNo()).getTableName();
        // 存储策略
        String policyPath = arSavePolicy.getPolicyPath();
        List<Integer> list = new ArrayList<>();
        List<String> stringList = new ArrayList<>();
        for (int i = 0; i >= 0 ;i++){
            int indexOf = policyPath.indexOf("@", i);
            list.add(indexOf);
            i = indexOf;
            if (i == -1){
                break;
            }
        }
        for (int i = 0; i < list.size() ;i+=2){
            if (list.get(i) == -1){
                break;
            }
            String substring = policyPath.substring(list.get(i), list.get(i + 1) + 1);
            stringList.add(substring);
        }
        for (String s : stringList) {
            String s1 = s.replaceAll("@","");
            String sql = sql(s1, tableName);
            policyPath = policyPath.replace(s,"/"+sql+"/");
            policyPath = policyPath.replaceAll("//","/");
        }
        arSavePolicy.setPolicyPath(policyPath);
        // 文件组成格式(图片命名)
        String fileFsmt = arSavePolicy.getFileFsmt();
        List<Integer> list1 = new ArrayList<>();
        List<String> stringList1 = new ArrayList<>();
        for (int i = 0; i >= 0 ;i++){
            int indexOf = fileFsmt.indexOf("@", i);
            list1.add(indexOf);
            i = indexOf;
            if (i == -1){
                break;
            }
        }
        for (int i = 0; i < list1.size() ;i+=2){
            if (list1.get(i) == -1){
                break;
            }
            String substring = fileFsmt.substring(list1.get(i), list1.get(i + 1) + 1);
            stringList1.add(substring);
        }
        for (String s : stringList1) {
            String s1 = s.replaceAll("@","");
            String sql = sql(s1, tableName);
            fileFsmt = fileFsmt.replace(s,"/"+sql+"/");
            policyPath = policyPath.replaceAll("//","/");
        }
        arSavePolicy.setFileFsmt(fileFsmt);
        // 新增
        arSavePolicy.setRecNo(UUID.randomUUIDPlus());
        arSavePolicy.setCreateTime(DateUtils.getNowDate());
        arSavePolicy.setCreateBy(SecurityUtils.getUsername());
        arSavePolicy.setDelFlag("0");
        arSavePolicyMapper.insertArSavePolicy(arSavePolicy);
        return AjaxResult.success();
    }

    private String sql(String s,String t) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //创建连接
            String url = "jdbc:mysql://192.168.1.77:3306/archives?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true";
            Connection conn = DriverManager.getConnection(url, "root", "123456");
            String sql = "select "+s+" from "+t+";";
            PreparedStatement stat = conn.prepareStatement(sql);
            ResultSet result = stat.executeQuery();
            String rs = null;
            // 获取列数
            ResultSetMetaData metaData = result.getMetaData();
            int columnCount = metaData.getColumnCount();
            // 遍历ResultSet中的每条数据
            while (result.next()){
                // 遍历每一列
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnLabel(i);
                    String value = result.getString(columnName);
                    rs = value;
                    if (StringUtils.isBlank(columnName)){
                        continue;
                    }

                }
            }
            stat.close();
            conn.close();
            return rs;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 修改存储策略信息
     * 
     * @param arSavePolicy 存储策略信息
     * @return 结果
     */
    @Override
    public int updateArSavePolicy(ArSavePolicy arSavePolicy)
    {
        // 验证是否有主表
        SysSort sysSort = sysSortMapper.selectSysSortById(arSavePolicy.getSortNo());
        String tableName = sysTableMapper.selectSysTableById(sysSort.getSortMainTableNo()).getTableName();
        // 存储策略
        String policyPath = arSavePolicy.getPolicyPath();
        List<Integer> list = new ArrayList<>();
        List<String> stringList = new ArrayList<>();
        for (int i = 0; i >= 0 ;i++){
            int indexOf = policyPath.indexOf("@", i);
            list.add(indexOf);
            i = indexOf;
            if (i == -1){
                break;
            }
        }
        for (int i = 0; i < list.size() ;i+=2){
            if (list.get(i) == -1){
                break;
            }
            String substring = policyPath.substring(list.get(i), list.get(i + 1) + 1);
            stringList.add(substring);
        }
        for (String s : stringList) {
            String s1 = s.replaceAll("@","");
            String sql = sql(s1, tableName);
            policyPath = policyPath.replace(s,"/"+sql+"/");
            policyPath = policyPath.replaceAll("//","/");
        }
        arSavePolicy.setPolicyPath(policyPath);
        // 文件组成格式(图片命名)
        String fileFsmt = arSavePolicy.getFileFsmt();
        List<Integer> list1 = new ArrayList<>();
        List<String> stringList1 = new ArrayList<>();
        for (int i = 0; i >= 0 ;i++){
            int indexOf = fileFsmt.indexOf("@", i);
            list1.add(indexOf);
            i = indexOf;
            if (i == -1){
                break;
            }
        }
        for (int i = 0; i < list1.size() ;i+=2){
            if (list1.get(i) == -1){
                break;
            }
            String substring = fileFsmt.substring(list1.get(i), list1.get(i + 1) + 1);
            stringList1.add(substring);
        }
        for (String s : stringList1) {
            String s1 = s.replaceAll("@","");
            String sql = sql(s1, tableName);
            fileFsmt = fileFsmt.replace(s,"/"+sql+"/");
            policyPath = policyPath.replaceAll("//","/");
        }
        arSavePolicy.setFileFsmt(fileFsmt);
        arSavePolicy.setUpdateTime(DateUtils.getNowDate());
        arSavePolicy.setUpdateBy(SecurityUtils.getUsername());
        return arSavePolicyMapper.updateArSavePolicy(arSavePolicy);
    }

    /**
     * 批量删除存储策略信息
     * 
     * @param recNos 需要删除的存储策略信息ID
     * @return 结果
     */
    @Override
    public int deleteArSavePolicyByIds(String[] recNos)
    {
        return arSavePolicyMapper.deleteArSavePolicyByIds(recNos);
    }

    /**
     * 删除存储策略信息信息
     * 
     * @param recNo 存储策略信息ID
     * @return 结果
     */
    @Override
    public int deleteArSavePolicyById(String recNo)
    {
        return arSavePolicyMapper.deleteArSavePolicyById(recNo);
    }

    /**
     * 根据分类编号查询存储策略路径
     * @param sortNo
     * @return
     */
    @Override
    public String selectArSavePolicyBySortId(String sortNo) {
        return arSavePolicyMapper.selectArSavePolicyBySortId(sortNo);
    }
}
